package br.com.vfsb.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.event.FileUploadEvent;

import br.com.vfsb.arquivo.LeituraBF;
import br.com.vfsb.util.UtilMsg;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

@ManagedBean(name = "MBUpload")
@ViewScoped
public class ControllerFileUpload implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 7702607894934475339L;
	private String destination = "\\arquivo\\";
	XStream x = new XStream(new DomDriver("ISO-8859-1"));
	//private Integer progress;

	public void handleFileUpload(FileUploadEvent event) {

		try {

			File ant = new File(destination + event.getFile().getFileName());
			File ren = new File(destination + "bf.xml");
			ant.renameTo(ren);
			// event.getFile().
			// event.getFile().getFileName().
			copyFile(ren.getName(), event.getFile().getInputstream());

		} catch (Exception e) {
			UtilMsg.Msg("Erro ao renomear arquivo: "
					+ event.getFile().getFileName());
			UtilMsg.Msg("ERRO: " + e);
		}
	}

	public void copyFile(String fileName, InputStream in) {
		try {

			// write the inputStream to a FileOutputStream
			OutputStream out = new FileOutputStream(new File(destination + fileName));

			int read = 0;
			byte[] bytes = new byte[1024];

			while ((read = in.read(bytes)) != -1) {
				out.write(bytes, 0, read);

			}

			in.close();

			out.flush();
			out.close();

			if (fileName.equals("bf.xml")) {

				LeituraBF lbf = new LeituraBF();
				// File a = new File(destination+fileName);
				// File b = new File(destination+"bf.xml");
				// a.renameTo(b);

				 org.primefaces.context.RequestContext.getCurrentInstance().execute("PF('pbAjax').start();");

				lbf.limparBF();

				lbf.Leitura();

				UtilMsg.Msg("Enviado com sucesso!");

				FacesContext.getCurrentInstance().getExternalContext()
						.redirect("bf.xhtml");

			} else if (fileName.equals("bi.xml")) {

				UtilMsg.Msg(" TESTE!");

			} else {

				UtilMsg.Msg("Arquivo " + fileName + " invalido! " + destination);

			}

		} catch (IOException e) {
			System.out.println("Arquivo Invalido");
		}
	}

	
	
	//progress bar teste
	/*
	public Integer getProgress() {

		if (progress == null) {
			progress = 0;

		} else {

			progress = progress + (int) (Math.random() * 35);

			if (progress >= 100)
				progress = 100;
		}

		return progress;
	}

	public void onComplete() {

		FacesContext.getCurrentInstance().addMessage(null,
				new FacesMessage("Progress Completed"));

	}

	public void cancel() {

		progress = null;
	}

	public void setProgress(Integer progress) {
		this.progress = progress;
	}

*/
}